<template>
	<scroll-view ref="scrollview" class="article-container" scroll-y :scroll-x="false" @scroll="handleScroll"
		:show-scrollbar="false">
		<!-- 自定义导航栏 -->
		<view class="custom-navbar" :class="{ visible: isNavVisible }" :style="{ marginTop: statusBarHeight + 'px' }">
			<view class="navbar-right">
				<text class="navbar-title" v-if="content.title">{{ content.title }}</text>
				<text class="navbar-date" v-if="formatDateString(content.date_by_day)">{{ formatDateString(content.date_by_day)
					}}</text>
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="article-header">
			<text class="date" v-if="formatDateString(content.date_by_day)">{{ formatDateString(content.date_by_day) }}</text>
			<text class="title" v-if="content.title">{{ content.title }}</text>
			<!-- <image src="/path/to/icon.png" class="icon"></image> -->
		</view>
		<view class="article-content" ref="articleContent">
			<view class="content" v-if="content.article">
				{{ content.article }}
			</view>
		</view>
		<view class="source" v-if="content.provenance && content.author.name"> 《{{ content.provenance }}》，{{
		content.author.name }} </view>
		<!-- <view>
			<text>阅读已完成，即可打卡</text>
		</view> -->
		<!-- 评论组件 -->
		<comments :articleId="articleId" :articleTitle="content.title" v-if="content.article" />
	</scroll-view>
	<interaction :isInteractionVisible="isInteractionVisible" :articleId="articleId" :articleTitle="content.title" />
</template>
<script setup>
import { formatDateString } from "@/util/index.js";
import { getContent } from "@/api/index.js";
import comments from "/components/comments/comments.vue"
import interaction from "/components/interaction/interaction.vue"
import {
	ref,
	onMounted,
	nextTick,
	reactive
} from 'vue';

import { onLoad } from "@dcloudio/uni-app";

const articleId = ref(0);

const content = reactive({
	article: '',
	author: {
		name: '',
	},
	provenance: '',
	title: '',
	date_by_day: ''
})




onLoad((options) => {
	articleId.value = options.articleId;
});


// 获取状态栏高度
// const statusBarHeight = ref(20); // 默认值
// 获取设备信息
const systemInfo = uni.getSystemInfoSync();
const windowHeight = systemInfo.windowHeight; // 视口高度
const statusBarHeight = systemInfo.statusBarHeight || 15; // 状态栏高度
// 控制导航栏的显示状态
const isNavVisible = ref(false);
// 控制 interaction-buttons 的显示状态
const isInteractionVisible = ref(false);
// 引用 DOM 元素
const scrollview = ref(null);
const articleContent = ref(null);
// 防抖函数
const debounce = (func, delay) => {
	let timer;
	return function (...args) {
		clearTimeout(timer);
		timer = setTimeout(() => func.apply(this, args), delay);
	};
};
// 滚动事件处理函数
const handleScroll = debounce((event) => {
	const scrollTop = event.detail?.scrollTop || 0; // 获取滚动位置
	// console.log('当前滚动位置:', scrollTop);
	// 控制 custom-navbar 的显示状态
	isNavVisible.value = scrollTop > 30;
	// 获取 article-content 的高度
	uni.createSelectorQuery().select('.article-content').boundingClientRect((contentRect) => {
		if (!contentRect) {
			// console.error('无法获取文章内容信息');
			return;
		}
		const contentHeight = contentRect.height; // 文章内容的高度
		const containerHeight = windowHeight; // 容器可见高度
		const scrollBottom = scrollTop + containerHeight; // 当前滚动底部位置
		// console.log('文章内容高度:', contentHeight);
		// console.log('容器可见高度:', containerHeight);
		// console.log('滚动底部位置:', scrollBottom);
		// 判断是否滚动到 article-content 的底部
		if (scrollBottom >= contentHeight && !isInteractionVisible.value) {
			// console.log('滚动到底部，显示 interaction-buttons');
			isInteractionVisible.value = true; // 显示按钮
		} else if (scrollBottom < contentHeight && isInteractionVisible.value) {
			// console.log('未滚动到底部，隐藏 interaction-buttons');
			isInteractionVisible.value = false; // 隐藏按钮
		}
	}).exec();
}, 100); // 防抖时间为 100ms
// 确保 DOM 已挂载后再操作
onMounted(async () => {
	// 获取文章内容

	uni.showLoading({ title: '加载中...', mask: true });
	const res = await getContent(articleId.value);

	// console.log("content:", res);
	Object.assign(content, {
		...res,
		// 新增强制类型转换
		date_by_day: String(res.date_by_day).padStart(8, '0')
	});

	uni.hideLoading();

	nextTick(() => {
		console.log('isNavVisible:', isNavVisible.value);
		console.log('isInteractionVisible:', isInteractionVisible.value);
	});
})
</script>
<style scoped>
/* 初始化样式 */
.container {
	font-family: Arial, sans-serif;
	background-color: #fff;
	margin: 0;
	padding: 0;
	height: auto;

	width: 100vw;
}

/* 自定义导航栏 */
.custom-navbar {
	position: fixed;
	top: -60px;
	left: 0;
	right: 0;
	height: 60px;
	/* padding-top: env(safe-area-inset-top, 30px); */
	/* padding-top: constant(safe-area-inset-top, 30px);	 */
	background-color: transparent;
	box-shadow: none;
	display: flex;
	align-items: center;
	justify-content: flex-end;
	padding: 0 20px;
	transform: translateY(-100%);
	transition: transform 0.3s ease-in-out;
	z-index: 1000;
}

.custom-navbar.visible {
	transform: translateY(0);
	background-color: rgba(255, 255, 255, 0.9);
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	top: 0;
}

.navbar-right {
	display: flex;
	flex-direction: column;
	align-items: flex-end;
}

.navbar-title {
	font-size: 18px;
	font-weight: bold;
	color: #333;
	height: 30px;
}

.navbar-date {
	font-size: 14px;
	color: #666;
	/* margin-top: 4px; */
	height: 30px;
}

/* 内容区域 */
.article-container {
	position: relative;
	padding: 30px 20px 0 20px;

	height: calc(100vh - 60px - env(safe-area-inset-top, 20px));
	margin-top: calc(v-bind('statusBarHeight + "px"'));
	overflow-y: auto;
	box-sizing: border-box;
	scrollbar-width: none;
	-ms-overflow-style: none;
	padding-bottom: 10px;
	/* margin-bottom: 50px; */
	/* padding-bottom: constant(safe-area-inset-bottom);
	    padding-bottom: env(safe-area-inset-bottom); */
}

.article-container::-webkit-scrollbar {
	display: none;
}

.article-header {
	display: flex;
	justify-content: flex-end;
	align-items: flex-start;
	margin-bottom: 100px;
	height: 200px;
}

.date,
.title {
	-webkit-writing-mode: vertical-lr;
	writing-mode: vertical-lr;
	text-orientation: upright;
	line-height: 1.5;
	letter-spacing: 4px;
	color: #333;
	text-align: right;
	display: block;
	font-size: 24px;
	font-weight: 500;
}

.date {
	font-weight: 0;
	font-size: 18px;
}

.icon {
	width: 30px;
	height: 30px;
}

.article-content {
	margin-bottom: 20px;
	min-height: 1px;
	/* 确保内容区域有高度 */
}


.content {
	font-size: 16px;
	line-height: 1.8;
}

.source {
	padding-bottom: 10px;
}
</style>